package com.hrt.web.comments.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Comments;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.HRTMember;
import com.hrt.bean.Replys;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.util.PageUtils;
import com.hrt.util.dateNum.NumDayDifference;
import com.hrt.web.comments.service.CommentsWebService;
import com.hrt.web.comments.service.ReplysWebService;
import com.hrt.web.product.service.GiftDetailService;

@Controller
@RequestMapping("HRTWEB/commentPage")
public class CommentsWebController extends BaseController<Comments>{
	
	//评论
	@Autowired
	private CommentsWebService commentsWebService;
	
	//礼品规格
	@Autowired
	private GiftDetailService giftDetailService;
	
	//会员
	@Autowired
	private HRTMemberService hRTMemberService;
	
	//回复
	@Autowired
	private ReplysWebService replysWebService;
	
	/**图片===》查看某礼品图片分页*/
	@RequestMapping(value = "/getCommentPicByGdeId")
	@ResponseBody
	public Map<String, PageUtils<Comments>> getCommentPicByGdeId(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "ids", required = true) String gdeIds,
			@RequestParam(value = "rankIdOption", required = true) String rankIdOption,
			HttpServletRequest request) {
		Map<String, PageUtils<Comments>> resultMap = new HashMap<String, PageUtils<Comments>>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		setTotalCount(commentsWebService.totalCount());
		List<Comments> resultList = new ArrayList<Comments>();
		Set<Comments> commentsSet = new HashSet<Comments>();
		String gdeIdstr = "";
		
		if(rankIdOption!=null){
			if (gdeIds == null) {
				throw new RuntimeException("礼品规格id有误");
			} else {
				//图片   --》根据礼品规格Id查找图片（picture!=null or picture!=""）的评论
				List<Comments> gdeIdsAddList = commentsWebService.findPicCommIdByGdeId(gdeIds);
				
				if(gdeIdsAddList!=null && gdeIdsAddList.size()>0){
					//根据图片的 会员id礼品id和订单id去查找 初评的信息
					for(Comments picSonId:gdeIdsAddList){
						Comments comments = this.commentsWebService.finCommByAddComm(picSonId.getHrtMemberId(),picSonId.getGdeId(),picSonId.getOrderId());
						commentsSet.add(comments);
					}
					//根据查找 初评的信息 --》去获取其Id并且合并为字符串
					for(Comments comm : commentsSet){
						gdeIdstr = gdeIdstr+comm.getId()+",";
					}
					//去除字符串的最后一个分号
					if(gdeIdstr != ""){
						gdeIdstr=gdeIdstr.substring(0,gdeIdstr.length()-1);
					}
					//分页
					resultList = commentsWebService.queryById(gdeIdstr, pageIndex, pageSize,rankIdOption);
					for(int i=0;i<resultList.size();i++){
						//转换时间的格式
						SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
						String commTime = sdf.format(resultList.get(i).getCommentTime());
						resultList.get(i).setCommTime(commTime);
						
						//多图片上传
						String picStr = resultList.get(i).getPicture();
						if(picStr!=null && picStr!="" ){
							String[] picStrArr = picStr.split(",");
							List<String> picList = new ArrayList<String>();
							for(int f = 0 ;f <picStrArr.length ; f++){
								picList.add(picStrArr[f]);
							}
							resultList.get(i).setPicStrs(picList);
						}
						
						//回复评论
						List<Replys> replyList = this.replysWebService.findReplyByCommId(resultList.get(i).getId());
						resultList.get(i).setReplys(replyList);
						
						//追评
						List<Comments> commentList = commentsWebService.finCommByMemIdAndGdeId(resultList.get(i).getHrtMemberId(),resultList.get(i).getGdeId(),resultList.get(i).getOrderId());
						if(commentList!=null && commentList.size()>0){
							for(int j=0;j<commentList.size();j++){
								//追评--》回复评论
								List<Replys> replyListAddCom = this.replysWebService.findReplyByCommId(commentList.get(j).getId());
								if(replyListAddCom!=null && replyListAddCom.size()>0){
									commentList.get(j).setReplys(replyListAddCom);
								}
								//多图片上传
								String addPicStr = commentList.get(j).getPicture();
								if(addPicStr!=null && addPicStr!="" ){
									String[] addPicStrArr = addPicStr.split(",");
									List<String> addPicList = new ArrayList<String>();
									for(int f = 0 ;f <addPicStrArr.length ; f++){
										addPicList.add(addPicStrArr[f]);
//										System.out.println(commentList.get(j).getId()+"==ll=="+addPicStrArr[f]);
									}
									commentList.get(j).setPicStrs(addPicList);
								}
								
								//追评--》转换时间的格式
								SimpleDateFormat sdfAdd = new SimpleDateFormat("yyyyMMdd");
								String commDayAdd = sdfAdd.format(commentList.get(j).getCommentTime());
								String commDaye = sdfAdd.format(resultList.get(i).getCommentTime());
								int  ret[]  =  NumDayDifference.getDateLength(commDaye, commDayAdd); 
								commentList.get(j).setCommTime(ret[2]+"");
							}
						}
						resultList.get(i).setAddComm(commentList);
						
					}
					/**根据规格id获取数量*/
					setTotalCount(commentsWebService.findNumByGdeId(gdeIdstr));
				}
			}
		}else{
			System.out.println("zzzz22222");
		}
		
		
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("pageResult", page);
		return resultMap;
	}
	
	/**追评===》查看某礼品追评分页*/
	@RequestMapping(value = "/getAddCommentByGdeId")
	@ResponseBody
	public Map<String, PageUtils<Comments>> getAddCommentByGdeId(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "ids", required = true) String gdeIds,
			@RequestParam(value = "rankIdOption", required = true) String rankIdOption,
			HttpServletRequest request) {
		Map<String, PageUtils<Comments>> resultMap = new HashMap<String, PageUtils<Comments>>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		setTotalCount(commentsWebService.totalCount());
		List<Comments> resultList = new ArrayList<Comments>();
		Set<Comments> commentsSet = new HashSet<Comments>();
		String gdeIdstr = "";
		if(rankIdOption!=null){
			if (gdeIds == null) {
				throw new RuntimeException("礼品规格id有误");
			} else {
				//追评   --》根据礼品规格Id查找追评（isAddComment=1）的评论
				List<Comments> gdeIdsAddList = commentsWebService.findAddCommIdByGdeId(gdeIds);
				if(gdeIdsAddList!=null && gdeIdsAddList.size()>0){
					//根据追评的 会员id礼品id和订单id去查找 初评的信息
					for(Comments addSonId:gdeIdsAddList){
						Comments comments = this.commentsWebService.finCommByAddComm(addSonId.getHrtMemberId(),addSonId.getGdeId(),addSonId.getOrderId());
						commentsSet.add(comments);
					}
					//根据查找 初评的信息 --》去获取其Id并且合并为字符串
					for(Comments comm : commentsSet){
						gdeIdstr = gdeIdstr+comm.getId()+",";
					}
					//去除字符串的最后一个分号
					if(gdeIdstr != ""){
						gdeIdstr=gdeIdstr.substring(0,gdeIdstr.length()-1);
					}
					//分页
					resultList = commentsWebService.queryById(gdeIdstr, pageIndex, pageSize,rankIdOption);
					for(int i=0;i<resultList.size();i++){
						//转换时间的格式
						SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
						String commTime = sdf.format(resultList.get(i).getCommentTime());
						resultList.get(i).setCommTime(commTime);
						
						//多图片上传
						String picStr = resultList.get(i).getPicture();
						if(picStr!=null && picStr!="" ){
							String[] picStrArr = picStr.split(",");
							List<String> picList = new ArrayList<String>();
							for(int f = 0 ;f <picStrArr.length ; f++){
								picList.add(picStrArr[f]);
							}
							resultList.get(i).setPicStrs(picList);
						}
						
						//回复评论
						List<Replys> replyList = this.replysWebService.findReplyByCommId(resultList.get(i).getId());
						resultList.get(i).setReplys(replyList);
						
						//追评
						List<Comments> commentList = commentsWebService.finCommByMemIdAndGdeId(resultList.get(i).getHrtMemberId(),resultList.get(i).getGdeId(),resultList.get(i).getOrderId());
						if(commentList!=null && commentList.size()>0){
							for(int j=0;j<commentList.size();j++){
								//追评--》回复评论
								List<Replys> replyListAddCom = this.replysWebService.findReplyByCommId(commentList.get(j).getId());
								if(replyListAddCom!=null && replyListAddCom.size()>0){
									commentList.get(j).setReplys(replyListAddCom);
								}
								
								
								//多图片上传
								String addPicStr = commentList.get(j).getPicture();
								if(addPicStr!=null && addPicStr!="" ){
									String[] addPicStrArr = addPicStr.split(",");
									List<String> addPicList = new ArrayList<String>();
									for(int f = 0 ;f <addPicStrArr.length ; f++){
										addPicList.add(addPicStrArr[f]);
//									System.out.println(commentList.get(j).getId()+"==ll=="+addPicStrArr[f]);
									}
									commentList.get(j).setPicStrs(addPicList);
								}
								
								//追评--》转换时间的格式
								SimpleDateFormat sdfAdd = new SimpleDateFormat("yyyyMMdd");
								String commDayAdd = sdfAdd.format(commentList.get(j).getCommentTime());
								String commDaye = sdfAdd.format(resultList.get(i).getCommentTime());
								int  ret[]  =  NumDayDifference.getDateLength(commDaye, commDayAdd); 
								commentList.get(j).setCommTime(ret[2]+"");
							}
						}
						resultList.get(i).setAddComm(commentList);
						
					}
					/**根据规格id获取数量*/
					setTotalCount(commentsWebService.findNumByGdeId(gdeIdstr));
				}
			}
		}
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("pageResult", page);
		return resultMap;
	}
	
	
	/**
	 * 查看某礼品的全部评论
	 * */
	/**全部评论分页*/
	@RequestMapping(value = "/findCommentByGdeId")
	@ResponseBody
	public Map<String, PageUtils<Comments>> findCommentByGdeId(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "ids", required = true) String gdeIds,
			@RequestParam(value = "rankIdOption", required = true) String rankIdOption,
			HttpServletRequest request) {
		
		Map<String, PageUtils<Comments>> resultMap = new HashMap<String, PageUtils<Comments>>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		setTotalCount(commentsWebService.totalCount());
		List<Comments> resultList = new ArrayList<Comments>();
		if(rankIdOption!=null){
			if (gdeIds == null) {
				throw new RuntimeException("礼品规格id有误");
			} else {
				
				List<Long> gdeIdsList = commentsWebService.findCommIdByGdeId(gdeIds);
				
				if(gdeIdsList!=null && gdeIdsList.size()>0){
					String gdeIdstr = "";
					for(Long sonId : gdeIdsList ){
						gdeIdstr = gdeIdstr+sonId+",";
					}
					if(gdeIdstr != ""){
						gdeIdstr=gdeIdstr.substring(0,gdeIdstr.length()-1);
					}
					
					/*resultList = commentsWebService.queryById(gdeIdstr, pageIndex,
							pageSize);*/
					resultList = commentsWebService.queryById(gdeIdstr, pageIndex,
							pageSize,rankIdOption);
					for(int i=0;i<resultList.size();i++){
						//转换时间的格式
//					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
						String commTime = sdf.format(resultList.get(i).getCommentTime());
						resultList.get(i).setCommTime(commTime);
						//多图片上传
						String picStr = resultList.get(i).getPicture();
						if(picStr!=null && picStr!="" ){
							String[] picStrArr = picStr.split(",");
							List<String> picList = new ArrayList<String>();
							for(int f = 0 ;f <picStrArr.length ; f++){
								picList.add(picStrArr[f]);
							}
							resultList.get(i).setPicStrs(picList);
						}
						
						
						//回复评论
						List<Replys> replyList = this.replysWebService.findReplyByCommId(resultList.get(i).getId());
						resultList.get(i).setReplys(replyList);
						
						//追评
						List<Comments> commentList = commentsWebService.finCommByMemIdAndGdeId(resultList.get(i).getHrtMemberId(),resultList.get(i).getGdeId(),resultList.get(i).getOrderId());
						if(commentList!=null && commentList.size()>0){
							for(int j=0;j<commentList.size();j++){
								
								//多图片上传
								String addPicStr = commentList.get(j).getPicture();
								if(addPicStr!=null && addPicStr!="" ){
									String[] addPicStrArr = addPicStr.split(",");
									List<String> addPicList = new ArrayList<String>();
									for(int f = 0 ;f <addPicStrArr.length ; f++){
										addPicList.add(addPicStrArr[f]);
//									System.out.println(commentList.get(j).getId()+"==ll=="+addPicStrArr[f]);
									}
									commentList.get(j).setPicStrs(addPicList);
								}
								
								//追评--》回复评论
								List<Replys> replyListAddCom = this.replysWebService.findReplyByCommId(commentList.get(j).getId());
								if(replyListAddCom!=null && replyListAddCom.size()>0){
									commentList.get(j).setReplys(replyListAddCom);
								}
								
								//追评--》转换时间的格式
								SimpleDateFormat sdfAdd = new SimpleDateFormat("yyyyMMdd");
								String commDayAdd = sdfAdd.format(commentList.get(j).getCommentTime());
								String commDaye = sdfAdd.format(resultList.get(i).getCommentTime());
								int  ret[]  =  NumDayDifference.getDateLength(commDaye, commDayAdd); 
								commentList.get(j).setCommTime(ret[2]+"");
							}
						}
						resultList.get(i).setAddComm(commentList);
						
					}
					/**根据规格id获取数量*/
					setTotalCount(commentsWebService.findNumByGdeId(gdeIdstr));
				}
			}
		}
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("pageResult", page);
		return resultMap;
	}
	
	/**
	 * 添加
	 * */
	
	@RequestMapping(value = "/saveWeb", method = RequestMethod.POST)
	public ModelAndView saveWeb(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		HttpSession session = request.getSession();
		String[] gdeId = request.getParameterValues("gdeId");//礼品id 
		String[] gdeName = request.getParameterValues("gdeName");//礼品名称 
		String[] picture = request.getParameterValues("picture");//多图片 
		String[] content = request.getParameterValues("content");//评论内容 
		String[] contentDet = request.getParameterValues("contentDet");//礼品规格评论
		String[] logistics = request.getParameterValues("logistics");//物流评论
		String[] servicerComm = request.getParameterValues("servicerComm");//客服评论
		String[] anonymous = request.getParameterValues("anonymous");//是否公开评论
		String[] orderId = request.getParameterValues("orderId");//订单详情id 
		if(gdeId!=null && gdeId.length>0){
			for(int i=0;i<gdeId.length;i++){
				Comments comments = new Comments();
				//礼品规格
				GiftDetail giftDetail =  this.giftDetailService.getById(Long.parseLong(gdeId[i]));
				comments.setGiftDetail(giftDetail);
				comments.setGdeId(Long.parseLong(gdeId[i]));
				comments.setGdeName(gdeName[i]);
				comments.setContent(content[i]);
				comments.setContentDet(Integer.parseInt(contentDet[i]));
				comments.setLogistics(logistics[i]);
				comments.setServicerComm(servicerComm[i]);
				comments.setAnonymous(Integer.parseInt(anonymous[i]));
				//多图片
				comments.setPicture(picture[i]);
				//会员
				HRTMember hrtMe = (HRTMember) session.getAttribute("HRTMember");
				if(hrtMe!=null){
					comments.setHrtMember(hrtMe);
					comments.setHrtMemberId(hrtMe.getId());
					comments.setHrtMemberName(hrtMe.getMbrName());
				}
				//订单的id
				comments.setOrderId(Long.parseLong(orderId[i]));
				//判断是否评论过
				List<Comments> addComm = this.commentsWebService.finNumByMemIdAndGdeId(hrtMe.getId(),Long.parseLong(gdeId[i]),Long.parseLong(orderId[i])); 
				if(addComm!=null && addComm.size()>0){
					comments.setIsAddComment(1);
				}else{
					comments.setIsAddComment(0);
				}
				//是否。。。。
//				comments.setIsAddComment(0);
				comments.setIsdelete(0);
				comments.setIsReply(0);
				//评论时间
				comments.setCommentTime(this.commentsWebService.getDate());
				commentsWebService.addComments(comments);
				mv.addObject(comments);
				mv.setViewName("redirect:/index.do");
			}
		}
		return mv;
	}
	
}
